#include "widget.h"
#include "ui_widget.h"

#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    m_settingManagerPtr = nullptr;

    m_VarSetManagerPtr = nullptr;

    m_ialgorithm_cnv2imgPlugin = nullptr;

    ui->setupUi(this);

}

Widget::~Widget()
{

    delete ui;
}

//  //  设置 参数管理器指针      //  //
void Widget::setSettingManagerPtr(OCTViewr2::SettingManager *ptr){
    m_settingManagerPtr=ptr;

    if(m_settingManagerPtr){

#ifdef QT_NO_DEBUG
//        if(m_settingManagerPtr->getGeneralLoginLevel()<OCTViewr2::LEVEL_Operator){
#endif
        QWidget *infobar=m_settingManagerPtr->getInfoToolBarWidget();
        if(infobar){
            infobar->setParent(this);
            ui->auxLayout->insertWidget(0,infobar);
        }
#ifdef QT_NO_DEBUG
//        }
#endif


    }
}

//  //  设置 变量集管理器指针      //  //
void Widget::setVarSetManagerPtr(OCTViewr2::VarSetManager *ptr)
{
    if(ptr){
        m_VarSetManagerPtr=ptr;
    }
}

//  // 处理显示之前做的东东   //  //
void Widget::doShowMisc()
{
    initPlugin();
    showNormal();

}

void Widget::initPlugin()
{
    if(m_settingManagerPtr){
        connect(this,SIGNAL(toastInfo(QString,QString)),
                m_settingManagerPtr,SLOT(toastInfo(QString,QString))
                );
    }else{
        return;
    }

    QPluginLoader pluginloader;
    QObject *objptr=nullptr;
    QVariantMap param_map;

    //load covert raw data to image algorithm plugin instance
    param_map.clear();
    m_settingManagerPtr->getParamInGroup(CstrParamPluginGroup,param_map);
    if(param_map.keys().contains(CstrParamCnv2ImgAlgorithmPluginPathName)){
        QString path=param_map[CstrParamCnv2ImgAlgorithmPluginPathName].toString();
        pluginloader.setFileName(path);
        objptr=pluginloader.instance();
        m_ialgorithm_cnv2imgPlugin=
                qobject_cast<OCTViewr2::IALGORITHM_CNV2IMG_INTERFACE*>(objptr);
        if(m_ialgorithm_cnv2imgPlugin){
            m_ialgorithm_cnv2imgPlugin->setSettingManagerPtr(m_settingManagerPtr);
            m_ialgorithm_cnv2imgPlugin->setVarSetManagerPtr(m_VarSetManagerPtr);
            m_ialgorithm_cnv2imgPlugin->initByGUIThread();
            toastInfo("ialgorithm_cnv2imgPlugin loaded. ","green");
        }else{
            toastInfo(pluginloader.errorString(),"red");
        }
    }

}

void Widget::on_BtnVarTool_clicked()
{
    if(m_VarSetManagerPtr){
        m_VarSetManagerPtr->execTuneDialog();
    }
}

void Widget::on_BtnTest1_clicked()
{
    if(!m_ialgorithm_cnv2imgPlugin){
        toastInfo("ialgorithm_cnv2imgPlugin not load!","red");
        return;
    }

    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open OCT Raw Data Image"),
                                                    "",
                                                    tr("PNG File (*.png)")
                                                    );
    if(fileName.isEmpty() || fileName.isNull()) return;

    auto imgmat = cv::imread(QDir::toNativeSeparators(fileName).toLocal8Bit().toStdString(),
                             cv::IMREAD_UNCHANGED
                             );

    if(!imgmat.data || imgmat.empty() || imgmat.type()!=CV_16UC1){
        toastInfo("Only support unsigned 16bit png!","red");
        return;
    }

    QImage qimg(fileName);
    qimg = qimg.scaled(ui->Disp1->width(),ui->Disp1->height());
    auto pixmap = QPixmap::fromImage(qimg);
    ui->Disp1->setPixmap(pixmap);

    // // 插件的调用
    OCTViewr2::RAW_IMAGE_DATA indata,outdata;
    indata.ptr = reinterpret_cast<char *>(imgmat.data);
    indata.rows = imgmat.rows;
    indata.cols = imgmat.cols;
    indata.depth = 0;
    indata.type = OCTViewr2::BUF_DATA_TYPE_U16C1;
    indata.totalByte = static_cast<size_t>(indata.rows * indata.cols) * 2;

    m_ialgorithm_cnv2imgPlugin->resetInterlBuffer(
                indata.cols,
                indata.rows,
                indata.type
                );

    m_ialgorithm_cnv2imgPlugin->receiveInputData(indata);
    m_ialgorithm_cnv2imgPlugin->execAlgorithmProcess();

    outdata.ptr = nullptr;
    m_ialgorithm_cnv2imgPlugin->getOutputDataShadowCopy(outdata);

    if(outdata.ptr && outdata.type == OCTViewr2::BUF_DATA_TYPE_U16C1){
        auto mat = cv::Mat(outdata.rows,outdata.cols,CV_16UC1,outdata.ptr);
        mat.convertTo(mat, CV_8U, 2/256.0);
        cv::cvtColor(mat,mat,cv::COLOR_GRAY2RGBA); //转换为RGBA彩色格式方便显示
        QImage i(mat.data,mat.cols,mat.rows,QImage::Format_RGBA8888);
        i = i.scaled(ui->Disp2->width(),ui->Disp2->height());
        auto p = QPixmap::fromImage(i);
        ui->Disp2->setPixmap(p);
    }
}
